package lobz.studio.findmyfriend;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.swarmconnect.Swarm;
import com.swarmconnect.SwarmActiveUser;
import com.swarmconnect.SwarmActiveUser.GotFriendsCB;
import com.swarmconnect.SwarmActivity;
import com.swarmconnect.SwarmMessage;
import com.swarmconnect.SwarmMessageThread;
import com.swarmconnect.SwarmMessageThread.GotMessagesCB;
import com.swarmconnect.SwarmMessageThread.SentMessageCB;
import com.swarmconnect.SwarmUser;
import com.swarmconnect.delegates.SwarmLoginListener;

public class ClientA extends SwarmActivity {
	TextView t_friendsCount;
	int friendsCount;
	TextView t_fromUser, t_messageContent;
	EditText e_messageContent;
	Button sendContent;
	private static int activeSessionID;
	LinearLayout ll;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_main);

		t_friendsCount = (TextView) findViewById(R.id.userCount);
		e_messageContent = (EditText) findViewById(R.id.chatWindow);
		//t_messageContent = (TextView) findViewById(R.id.textContent);
	//	t_fromUser = (TextView) findViewById(R.id.fromUser);
		
		sendContent = (Button)findViewById(R.id.sendB);
		sendContent.setOnClickListener(bSendListener);
		
		ScrollView sv = new ScrollView(this);
		ll = (LinearLayout)findViewById(R.id.lista);
		ll.setOrientation(LinearLayout.VERTICAL);
		//sv.addView(ll);

		Swarm.setAllowGuests(true);
		Swarm.init(this, 7316, "5697a3956908667894062d02d905e730",
				swarmLoginListener);

		// Swarm.showDashboard();

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (!(Swarm.isInitialized())) {
			Swarm.setAllowGuests(true);
			Swarm.init(this, 7316, "5697a3956908667894062d02d905e730",
					swarmLoginListener);
		}
	}

	private SwarmLoginListener swarmLoginListener = new SwarmLoginListener() {

		@Override
		public void userLoggedOut() {
			// TODO Auto-generated method stub

		}

		@Override
		public void userLoggedIn(SwarmActiveUser arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(ClientA.this, "User logged " + arg0,
					Toast.LENGTH_SHORT).show();

			if (Swarm.isLoggedIn()) {

				// Get the user's friend list.
				Swarm.user.getFriends(new GotFriendsCB() {
					public void gotFriends(List<SwarmUser> friends) {
						String users;
						StringBuilder sB = null;
						users = t_friendsCount.getText().toString();
						if (friends != null) {
							friendsCount = 0;
							for (SwarmUser friend : friends) {
								sB = new StringBuilder(users);
								sB.append(friend.username + " ");
								friendsCount++;
								// See below for function
								// getFriendStatus(friend);
								activeSessionID = friend.userId;
							}
							t_friendsCount.setText("You have: " + friendsCount
									+ " friends!" + "  ( " + sB.toString()
									+ " )");
						} else {
							t_friendsCount.setText("You have: " + friendsCount
									+ " friends =( !");
						}
					}
				});
				SwarmMessageThread sT = new SwarmMessageThread();
				sT.getAllThreads(cbThreads);
				//sT.getMessages(cbMessages);

			}
		
			
		
		
		}

		@Override
		public void loginStarted() {
			// TODO Auto-generated method stub

		}

		@Override
		public void loginCanceled() {
			// TODO Auto-generated method stub

		}
	};

	SwarmMessageThread.GotThreadsCB cbThreads = new SwarmMessageThread.GotThreadsCB() {
		
		@Override
		public void gotThreads(List<SwarmMessageThread> arg0) {
			Toast.makeText(ClientA.this, arg0.get(arg0.size()-1).lastMessage, Toast.LENGTH_SHORT).show();
			
		}
	};

SwarmMessageThread.GotMessagesCB cbMessages = new GotMessagesCB() {
		
		@Override
		public void gotMessages(List<SwarmMessage> messages) {
			if (messages.size()>0) {
			Toast.makeText(ClientA.this, messages.get(messages.size()).message, Toast.LENGTH_SHORT).show();
			}
		}
	};
	
	SwarmMessageThread.SentMessageCB sendMessageCB = new SentMessageCB() {
		
		@Override
		public void sentMessage(boolean arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void sendFailed() {
			// TODO Auto-generated method stub
			Toast.makeText(ClientA.this, "send failed", Toast.LENGTH_SHORT).show();
		}
	};
	
	
	View.OnClickListener bSendListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (Swarm.isLoggedIn()) {

				if (e_messageContent.getText().length()>0) {
				SwarmMessage.sendMessage(activeSessionID,
						e_messageContent.getText().toString(), sendMessageCB);
				
				Toast.makeText(ClientA.this, "sended", Toast.LENGTH_SHORT).show();
				
				t_messageContent = new TextView(ClientA.this);
				t_messageContent.setText("You: "+e_messageContent.getText().toString());
				 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				 t_messageContent.setLayoutParams(lp);
				ll.addView(t_messageContent);
				e_messageContent.setText("");
				ll.invalidate();
				}
			}
		}
	};
}
